package com.mjk.common.tools.gson.adapter.impl;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

public class StringAdapterImpl extends TypeAdapter<String> {

	@Override
	public void write(JsonWriter out, String value) throws IOException {
		if (value == null) {
			out.value("");
			return;
		}
		out.value(value);
	}

	@Override
	public String read(JsonReader in) throws IOException {
		if (in.peek() == JsonToken.NULL) {
			in.nextNull();
			return "";
		}
		return in.nextString();
	}
}
